package com.yt.fras.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yt.fras.mapper.UserMapper;
import com.yt.fras.pojo.entity.Permission;
import com.yt.fras.pojo.entity.User;
import com.yt.fras.pojo.model.UserInfo;
import com.yt.fras.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author yt
 * @since 2021-10-25
 */
@Service
@CacheConfig(cacheNames = "userCache")
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public boolean updatePassword(String id, String password) {
        User user = userMapper.selectById(id);
        user.setPassword(password);
        return updateById(user);
    }

    @Override
    public boolean resetPassword(String username, String password) {
        User user = queryByUsername(username);
        user.setPassword(password);
        return updateById(user);
    }

    @Override
    public boolean verificationUsername(String username) {
        User user = queryByUsername(username);
        return user == null ? true : false;
    }

    @Override
    public boolean verificationEmail(String username, String email) {
        User user = queryByUsername(username);
        return user.getEmail().equals(email) ? true : false;
    }

    @Override
    public List<Permission> findPermissionByUsername(String username) {
        return userMapper.findPermissionByUsername(username);
    }

    @Override
    public User queryByUsername(String username) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        User user = userMapper.selectOne(wrapper);
        return user;
    }

    @Override
    public List<UserInfo> getUserInfoList() {
        return userMapper.getUserInfoList();
    }

    @Override
    public List<UserInfo> getUserInfoListByGroup(String groupId) {
        return userMapper.getUserInfoListByGroup(groupId);
    }

    @Override
    public UserInfo getUserInfoByUsername(String username) {
        return userMapper.getUserInfoByUsername(username);
    }

}
